package shujujiegou;

import java.util.Scanner;

public class message {
    // 内部类 Friend 表示双链表中的节点，存储联系人信息
    public class Friend {
        // 指向前一个节点的引用
        public Friend prev;
        // 指向后一个节点的引用
        public Friend next;
        private String Name;
        private String Tel;
        private String Email;
        private String BornAdd;// 无参构造函数，用于创建一个空的联系人节点
        public Friend() {
        }

        // 有参构造函数，用于创建一个包含指定信息的联系人节点
        public Friend(String Name, String Tel, String Email, String BornAdd) {
            this.Name = Name;
            this.Tel = Tel;
            this.Email = Email;
            this.BornAdd = BornAdd;
        }

        public String getName() {
            return Name;
        }

        public void setName(String Name) {
            this.Name = Name;
        }

        public void setTel(String Tel) {
            this.Tel = Tel;
        }

        public String getTel() {
            return Tel;
        }

        public void setEmail(String Email) {
            this.Email = Email;
        }

        public String getEmail() {
            return Email;
        }

        public void setBornAdd(String BornAdd) {
            this.BornAdd = BornAdd;
        }

        public String getBornAdd() {
            return BornAdd;
        }

        public void message(String Name, String Tel, String Email, String BornAdd) {
            this.Name = Name;
            this.Tel = Tel;
            this.Email = Email;
            this.BornAdd = BornAdd;
        }

        // 重写 toString 方法，打印联系人信息
        public String toString() {
            return this.Name + "\t" + this.Tel + "\t" + this.Email + "\t" + this.BornAdd;
        }
    }

    // 双链表中节点的数量
    private int size;
    // 双链表的头节点
    private Friend head;
    // 双链表的尾节点
    private Friend tail;

    // 初始化双链表，节点数量置为 0，头节点和尾节点置为 null
    {
        this.size = 0;
        this.head = null;
        this.tail = null;
    }

    // 在链表头部插入新节点
    public void insertHead(Friend s) {
        if (this.size == 0) {
            // 若链表为空，新节点既是头节点也是尾节点
            this.head = s;
            this.tail = s;
        } else {
            // 新节点的 next 指向原头节点
            s.next = this.head;
            // 原头节点的 prev 指向新节点
            this.head.prev = s;
            // 新节点成为头节点
            this.head = s;
        }
        this.size++;
    }

    // 根据姓名删除联系人节点
    public boolean delete(String Name) {
        Friend current = this.head;
        while (current != null) {
            if (Name.equals(current.getName())) {
                if (current.prev != null) {
                    // 若当前节点不是头节点，调整前一个节点的 next 指针
                    current.prev.next = current.next;
                } else {
                    // 若当前节点是头节点，更新头节点
                    this.head = current.next;
                }
                if (current.next != null) {
                    // 若当前节点不是尾节点，调整后一个节点的 prev 指针
                    current.next.prev = current.prev;
                } else {
                    // 若当前节点是尾节点，更新尾节点
                    this.tail = current.prev;
                }
                this.size--;
                return true;
            }
            current = current.next;
        }
        return false;
    }

    // 根据姓名查找联系人节点
    public Friend select(String Name) {
        Friend front = this.head;
        while (front != null) {
            if (Name.equals(front.getName())) {
                return new Friend(front.getName(), front.getTel(), front.getEmail(), front.getBornAdd());
            }
            front = front.next;
        }
        return null;
    }

    // 显示链表中所有联系人信息
    public void display() {
        System.out.println("姓名\t 手机号\t 邮箱地址\t 家庭住址");
        Friend front = this.head;
        while (front != null) {
            System.out.println(front.toString());
            front = front.next;
        }
        if (this.head == null) {
            System.out.println("无相关信息");
        }
    }

    public static void main(String[] args) {
        message sm = new message();
        Scanner in = new Scanner(System.in);
        boolean open = true;
        while (open) {
            printMenu();
            System.out.println("请输入菜单序号进行操作");
            int menu = -1;
            try {
                menu = in.nextInt();
            } catch (Exception e) {
                in = new Scanner(System.in);
            }
            switch (menu) {
                case 0: {
                    System.out.println("创建成功");
                }
                break;
                case 1: {
                    System.out.println("请输入姓名");
                    String Name1 = in.next();
                    System.out.println("手机号，邮箱地址，宿舍地址");
                    try {
                        String Tel = in.next();
                        String Email = in.next();
                        String BornAdd = in.next();
                        Friend news = sm.new Friend(Name1, Tel, Email, BornAdd);
                        sm.insertHead(news);
                    } catch (Exception e) {
                        System.out.println("输入信息有误");
                        in = new Scanner(System.in);
                    }
                    break;
                }
                case 2: {
                    System.out.println("请输入要查询联系人的姓名");
                    String Name2 = in.next();
                    Friend selectS = sm.select(Name2);
                    System.out.println("姓名\t 手机号\t 邮箱地址\t 宿舍地址");
                    if (selectS == null) {
                        System.out.println("未找到相关联系人");
                    } else {
                        System.out.println(selectS.toString());
                    }
                    break;
                }
                case 3: {
                    System.out.println("请输入要删除联系人的姓名");
                    String Name3 = in.next();
                    if (sm.delete(Name3)) {
                        System.out.println("删除成功");
                    } else {
                        System.out.println("删除失败");
                    }
                    sm.display();
                    break;
                }
                case 4: {
                    sm.display();
                    break;
                }
                case 5: {
                    System.out.println("系统已退出");
                    System.exit(0);
                    break;
                }
                default:
                    System.out.println("输入错误");
            }
        }
        in.close();
    }

    public static void printMenu() {
        System.out.println("24软件1班宿舍管理系统");
        System.out.println("*****************");
        System.out.println("* 0----创建链表   *");
        System.out.println("* 1----插入联系人 *");
        System.out.println("* 2----查找联系人 *");
        System.out.println("* 3----删除联系人 *");
        System.out.println("* 4----输出通讯录 *");
        System.out.println("* 5----退出系统   *");
        System.out.println("*****************");
        System.out.println("*本程序由P20240310016黄可儿设计并拥有全部代码版权*");
    }
}
//黄可儿 15361906101 780202861@qq.com 20栋B1013